﻿<!DOCTYPE html>
<html>
<head>
  <title>SVG Images using Data URL GoJS Sample</title>
  <!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
  <meta name="description" content="Export SVG with Base64 URIs instead of URLs for picture hrefs." />
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="../release/go.js"></script>
  <script src="../assets/js/goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->
  <script id="code">
    function init() {
      if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this

      var $ = go.GraphObject.make;  // for conciseness in defining templates

      myDiagram = $(go.Diagram, "myDiagramDiv",  // create a Diagram for the DIV HTML element
                    {
                      "undoManager.isEnabled": true  // enable undo & redo
                    });

      // define a simple Node template
      myDiagram.nodeTemplate =
        $(go.Node, "Auto",  // the Shape will go around the TextBlock
          $(go.Shape, "RoundedRectangle", { strokeWidth: 0 },
            // Shape.fill is bound to Node.data.color
            new go.Binding("fill", "color")),
          $(go.Picture, { margin: 8, width: 55, height: 55 },
            new go.Binding("source"))
        );

      // create the model data that will be represented by Nodes and Links
      myDiagram.model = new go.GraphLinksModel(
      [
        { key: "Alpha", source: 'images/55x55.png', color: "lightblue" },
        { key: "Beta",  source: 'images/55x55.png', color: "orange" },
        { key: "Gamma", source: 'images/55x55.png', color: "lightgreen" },
        { key: "Delta", source: 'images/55x55.png', color: "pink" }
      ],
      [
        { from: "Alpha", to: "Beta" },
        { from: "Alpha", to: "Gamma" },
        { from: "Beta", to: "Beta" },
        { from: "Gamma", to: "Delta" },
        { from: "Delta", to: "Alpha" }
      ]);


    } // end init

    function toDataURL(url, callback) {
      var xhr = new XMLHttpRequest();
      xhr.onload = function() {
        var reader = new FileReader();
        reader.onloadend = function() {
          callback(reader.result);
        }
        reader.readAsDataURL(xhr.response);
      };
      xhr.open('GET', url);
      xhr.responseType = 'blob';
      xhr.send();
    }

    // Make SVG, but modify the SVG <image> Element's href to refer to a Base64 URI instead of the go.Picture source URL.
    function makeSvg() {
      var svg = myDiagram.makeSvg({
        elementFinished: function(graphobject, svgelement) {
          if (!(graphobject instanceof go.Picture)) return;
          toDataURL(svgelement.href.baseVal, function(dataUrl) {
            svgelement.setAttribute('href', dataUrl);
          });
        }
      });
      document.getElementById('SVGResult').appendChild(svg);
    }


  </script>
</head>
<body onload="init()">
<div id="sample">
  <div id="myDiagramDiv" style="border: solid 1px black; width:300px; height:300px"></div>
  <button onclick="makeSvg()">Make SVG</button>
  <div id="SVGResult"></div>
  <p>
    This sample shows how to setup an <code>elementFinished</code> option for <a>Diagram.makeSvg</a> that will replace the
    SVG <code>&lt;image&gt;</code> tag's <code>href</code> with a Base64 URI instead of pointing to the <a>Picture.source</a>.
    This can be useful to reduce external dependencies within your exported SVG.
  </p>
  <p>
    This method will only work if you assets are at the same origin,
    or otherwise not blocked by a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">cross-origin request policy</a>.
  </p>
</div>
</body>
</html>